package com.mgxy.GlodMiner;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GameWin extends JFrame {

    //游戏状态 0未开始 1运行中 2商店 3失败 4 胜利
    static int gamestate;

    //存储金块，石头
    ArrayList<Object> objectList = new ArrayList<Object>();
    Bg bg = new Bg();
    Line line = new Line(this);

    {
        //是否可以放置
        boolean isPlace = true;
        for (int i = 0; i < 11; i++) {
            double random =Math.random();
            Gold gold;//存放当前生成的金块
            if(random<0.3){gold = new GoldMini();}
            else if(random<0.7){ gold = new Gold();}
            else { gold = new GoldPlus();}

            for(Object obj:objectList){
                if(gold.getRec().intersects(obj.getRec())){
                    //不可放置,需要重新生成
                    isPlace=false;
                }
            }
            if(isPlace){objectList.add(gold);}
            else {isPlace=true;i--;}
        }
        for (int i = 0; i < 5; i++) {
            Rock rock=new Rock();
            for(Object obj:objectList){
                if(rock.getRec().intersects (obj.getRec()))
                { isPlace=false; }
            }
            if(isPlace){ objectList.add(rock); }
            else{isPlace=true;i--;}
        }
    }

    Image offScreenImage;

    void launch(){
        //设置窗口
        this.setVisible(true);
        this.setSize(768,1000);
        this.setLocationRelativeTo(null);
        this.setTitle("黄金矿工----By MG斜阳");
        //窗口不可变
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //添加鼠标监听事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (gamestate){
                    case 0:
                        if (e.getButton()==3){
                            gamestate=1;
                            bg.startTime=System.currentTimeMillis();
                        }
                        break;
                    case 1:
                        //左右摇摆,点击左键
                        if(e.getButton()==1 && line.state==0){line.state=1;}
                        //抓取返回,点击右键
                        if (e.getButton()==3 && line.state==3 && Bg.waterNum>0){
                            Bg.waterNum--;
                            Bg.waterFlag=true;
                        }
                        break;
                    case 2:
                        if(e.getButton()==1){
                            bg.shop=true;
                        }
                        if (e.getButton()==3){
                            gamestate=1;
                            bg.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 3:
                    case 4:
                        if(e.getButton()==1){
                            gamestate=0;
                            bg.reGameBg();
                            line.reGameLine();
                        }
                        break;
                    default:
                }
            }
        });
        //添加键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int code = e.getKeyCode();
                if (code == 40 && line.state==0){line.state=1;}
                if (code == 38 && line.state==3){
                    if (Bg.waterNum>0){
                        Bg.waterNum--;
                        Bg.waterFlag=true;
                    }
                }
            }
        });

        //刷新画布
        while (true){
            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    //下一关
    public void nextLevel(){
        if(bg.gameTime() && gamestate==1){
            if(Bg.count >= bg.goal)
            {
                if(Bg.level==50) {
                    gamestate=4;
                }
                else {
                    gamestate=2;
                    Bg.level++;
                }
            }else {gamestate=3;}
            dispose();
            GameWin gameWin1 = new GameWin();
            gameWin1.launch();
        }

    }

    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(768,1000);
        Graphics gImage = offScreenImage.getGraphics();
        bg.paintSelf(gImage);

        if(gamestate==1) {
            //绘制物体
            for (Object obj : objectList) {
                obj.paintSelf(gImage);
            }
            line.paintSelf(gImage);
        }
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
